import java.util.*;

/**
* @description: 811. 子域名访问计数
* @author hewei
* @date 2022/8/29 15:34
* @version 1.0
*/

public class SubdomainVisits {

    public List<String> subdomainVisits(String[] cpdomains) {
        HashMap<String, Integer> map = new HashMap<>();
        for (String s : cpdomains) {
            int i = 0;
            int num = 0;
            while (s.charAt(i) != ' ') {
                num = num * 10 + (s.charAt(i++) - '0');
            }
            String temp = s.substring(++i);
            map.put(temp, map.getOrDefault(temp, 0) + num);
            temp = temp.substring(temp.indexOf(".") + 1);
            map.put(temp, map.getOrDefault(temp, 0) + num);
            if (temp.contains(".")) {
                temp = temp.substring(temp.indexOf(".") + 1);
                map.put(temp, map.getOrDefault(temp, 0) + num);
            }
        }
        List<String> ans = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            ans.add(entry.getValue() + " " + entry.getKey());
        }
        return ans;
    }
}
